import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
import Users from '../components/user/Users'
import Admins from '../components/user/Admins'
import Rights from '../components/power/Rights'
import Roles  from '../components/power/Roles'
import Category from '../components/good/Cate'
import Params from '../components/good/Params'
import GoodList from '../components/good/GoodList'
import AddGood from '../components/good/Add'
import Order from '../components/order/Order'
import Report from '../components/report/Report'
import Problems from '../components/problem/Problems'
import Auths from '../components/auth/Auths'
import Submits from '../components/submit/Submits'
import AddProblem from '../components/problem/AddProblem'

Vue.use(VueRouter)

const routes = [
  { path: '/login', component: Login},
  
  { path: '/home', 
    component: Home , 
    redirect: '/welcome',
    children:[
      {path: '/welcome', component: Welcome},
      {path: '/users',component: Users},
      {path: '/admins',component: Admins},
      {path: '/rights', component: Rights},
      {path: '/roles', component: Roles},
      {path: '/categories', component: Category},
      {path: '/params', component: Params},
      {path: '/goods',component:GoodList},
      {path: '/goods/add',component:AddGood},
      {path: '/orders',component:Order},
      {path: '/statistics',component:Report},
      {path: '/problems',component:Problems},
      {path: '/auths',component:Auths},
      {path: '/submits',component:Submits},
      {path: '/addproblem',component:AddProblem},
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


// 挂载路由守卫 ，也就是拦截
router.beforeEach((to, from, next) => {
    // to 将要访问的页面
    // from 从那个页面访问来的
    // next 放行函数，调用这个函数直接放行  next('/login') 强制跳转到 login页面

    if(to.path === '/login')
    {
      return next() 
    }

    const tokenStr = window.sessionStorage.getItem("token")

    if ( !tokenStr ){
      return next('/login')
    } 
    next();

})




export default router
